home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1997 May
/
EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso
/
softwareupdate
/
system
/
amigados
/
parsingcommandline
/
functions.doc
< prev
next >
Wrap
Text File
|
1996-10-10
|
14KB
|
368 lines
5 PARSING THE COMMAND LINE - FUNCTIONS
5 QUICK REFERENCE
Here is a complete list of all parsing routines:
Function Description
-------------------------------------------------------------
FindArg() Looks for names in a command line template
FreeArgs() Deallocates memory after ReadArgs()
ReadArgs() Parses the command line or a user string
ReadItem() Collects arguments from command line or string
-------------------------------------------------------------
---------------------------------------------------------------
FindArg()
ROM library: "dos.library/FindArg", (V36+)
#include <clib/dos_protos.h>
Examines a command line template and looks for a specified
command template name. Returns the command template position,
or -1 if the command template name could not be found.
Synopsis: position = FreeArgs( name, template );
position: (LONG) If the function finds the command template
it will return the command template's position
in the complete command line template. If the
command template could not be found -1 is returned.
name: (STRPTR) Pointer to a string containing the name
of the command template you want to look for.
template: (STRPTR) Pointer to a string containing the
command line template you want to search in.
Here is a simple example on how to use the FindArg() function:
("SoundFiles" has the position 0, "Volume" position 1, and
"Filter" position 2.)
#define MY_TEMPLATE "SoundFiles/M/A,V=Volume/K/N,F=Filter/S"
/* Store the command template position here: */
int template_position;
- - -
/* Which position has the comman template "Filter"? */
template_position = FindArg( MY_COMMAND_LINE_TEMPLATE, "Filter" );
/* Print the position: */
printf( "Position %d\n", template_position );
See also: AllocDosObject(), FreeArgs(), FreeDosObject(),
ReadArgs(), ReadItem()
---------------------------------------------------------------
FreeArgs()
ROM library: "dos.library/FreeArgs", (V36+)
#include <clib/dos_protos.h>
Deallocates the memory that was allocated when you successfully
called the ReadArgs() function.
Synopsis: FreeArgs( rdargs );
retrda: (struct RDArgs *) Pointer to the RDArgs structure
that was returned when you successfuly called
ReadArgs().
If you have allocated the RDArgs structure yourself
only the collected arguments will be deallocated. The
structure itself must then be deallocated with help
of the FreeDosObject() function.
If you did not allocate the RDArgs structure (it was
automatically allocated by the ReadArgs() function)
the structure as well as the data for the arguments
will be deallocated.
Please remember that before your program terminates you must
always call FreeArgs() if you have successfully used
ReadArgs(), even if you have allocated the RDArgs structure
yourself.
Here is a simple example on how to use the FreeArgs() function:
/* Store the returned RDArgs pointer here: */
struct RDArgs *returned_rdargs;
- - -
/* Parse the command line: */
returned_rdargs = ReadArgs( MY_TEMPLATE, arg_array, NULL );
- - -
/* Free the arguments: */
FreeArgs( my_rdargs );
See also: AllocDosObject(), FindArg(), FreeDosObject(),
ReadArgs(), ReadItem()
---------------------------------------------------------------
ReadArgs()
ROM library: "dos.library/ReadArgs", (V37+)
#include <clib/dos_protos.h>
Parses the command line or an own defined command string with
the help of a command line template.
Synopsis: retrda = ReadArgs( cltemplate, arg_array, rdargs );
retrda: (struct RDArgs *) If the function could
successfully parse the command line (or our own
command string) it returns a pointer to a
RDArgs structure. If we supplied the function
with our own RDArgs structure, this pointer will
simply point to our structure. On the other hand,
if we did not supply the function with an already
created RDArgs structure the function will have
created one for us.
If this function manages to parse the command
line (or our own command string), regardless if
you have created the RDArgs structure yourself or
not, you must call the FreeArgs() function when
you do not need to use the collected arguments
any more. (If you allocated the RDArgs structure
yourself you must then also free it yourself
with help of the FreeDosObject() function.)
If the function fails to parse the command line
(or our own command string) it returns NULL.
cltemplate: (STRPTR) Pointer to a sting which contains our
"command line template". Each "command template"
is written like this:
[<abbreviation>=]TemplateName[option(s)]
First there is the optional abbreviation, then
comes the name of this command template, and
finally zero or more of the following options:
(See chapter document for more information about
the comman templates.)
Option Description
----------------------------------
/A Always required argument
/F Final argument of the line
/K Keyword is required
/M Multiple arguments
/N Number argument
/S Switch argument
/T Toggle argument
If there are more than one command template they
must be separated with commas. No spaces are
allowed. Please note that the parse functions are
not case sensetive, the small "a" is equal to the
captial "A".
arg_array: (LONG *) You must give the function an array of
"LONG" variables, with at least one entry (LONG
variable) for every command template. If the
function manages to parse the command line (or
ur our own command string) the result of each
command template will be placed in the
corresponding position in array. (The result of
the first command template will be placed in
position 0 of the array, the second command
template will be placed in position 1 in the
array, and so on...
Note! The array must have one entry (one LONG
variable) for every comman template! The function
will not check the size of the array before it
stores the result in it.
What type of values that will be stored in the
array depends on what type of command templates
are used.
1. The default is that the array will contain a
pointer to a string where a copy of the
corresponding argument is placed. If no argument
of this type was entered the pointer will be
NULL. (Remember to check that the pointer really
points to a string before you try to examine the
string! If the "/A" option was used together with
this template we know that the pointer must point
to a string since this argument was required, and
the function would have failed if this argument
was not included. However, it it best to always
check the string that it does not point to NULL
before you use it!)
2. If the "/M" (multiple arguments) option was
used there may be zero or more arguments that
fitted this template. The LONG value in the array
will therefore consist of a pointer to an array
of strings, where the last string pointer is set
to NULL.
3. If the "/N" (numeric argument) option was used
the LONG variable in the array will contain a
pointer to the value, or be NULL if no value was
entered.
4. Finally there exist the switches "/S" (or
"flags" as they are sometimes called). If the
switch was set the variable in the array will be
non-zero. If the switch was not set the value
will be 0. If you used the "/T" switch the value
in the array will toggle if the argument was set.
(If the value in the array was 0 it will be
toggled to a non-zero value, and if the value
was non-zero it will be togled to 0.)
rdargs: (struct RDArgs *) If you want to set some special
flags or parse your own command string (instead
of the default command line) you should create a
RDArgs structure yourself and prepare it as
desired, before you call this function and pases
a pointer to the structure. However, if you want
to parse the command line and do not want to set
any special flags you simply write NULL.
Note that if this function could successfully parse the command
line you have to call the FreeArgs() function later on when you
do not need the arguments any more. This must be done regardles
of you have created the RDArgs structure yourself or not.
When the ReadArgs() function successfully manages to parse a
command line (or our own command string) it will allocate some
memory where the result (data) will be stored. Remember that the
array you use only contains pointers to the data, and it is this
data areas that must be deallocated. If the ReadArgs() function
fails it will have not allocated any memory to store the data
in, and consequently you do not have to call the FreeArgs()
function.
Here is a simple example on how to use the ReadArgs() function:
#define MY_TEMPLATE "SoundFiles/M/A,V=Volume/K/N,F=Filter/S"
/* Store the returned RDArgs pointer here: */
struct RDArgs *returned_rdargs;
- - -
/* Parse the command line: */
returned_rdargs = ReadArgs( MY_TEMPLATE, arg_array, NULL );
/* Could we parse the command line? */
if( returned_rdargs )
{
printf( "The command line successfully parsed!\n" );
/* Examine the arg_array to see what arguments were set: */
}
else
printf( "Could not parse the command line!\n" );
See also: AllocDosObject(), FindArg(), FreeArgs(),
FreeDosObject(), ReadItem()
---------------------------------------------------------------
ReadItem()
ROM library: "dos.library/ReadItem", (V36+)
#include <clib/dos_protos.h>
Collects one argument from the command line or our own command
string.
Synopsis: item = ReadItem( buffer, size, source );
item: (LONG) The function will return a value which tells
us what type of argument that was collected. For
the moment these types of items can be identified:
(defined in headerfile "dos/dos.h")
ITEM_NOTHING Nothing found, no more arguments
(items) in the command line/string.
ITEM_UNQUOTED The collected argument (item) was not
surrounded by quotation marks.
ITEM_QUOTED The collected argument was surrounded
by quotation marks.
ITEM_EQUAL The collected argument was an equal
sign (=).
ITEM_ERROR There was a problem, could not
collect the argument.
buffer: (STRPTR) Pointer to some memory where the collected
argument can be stored.
size: (LONG) The size of the memory where the collected
argument will be stored.
source: (struct CSource *) If you want to collect the
argument from the command line set this field to
NULL. If you want to collect the argument from a
a string you have created yourself give this field
a pointer to a CSource structure which you have
prepared with the command string you want to use.
Here is a simple example on how to collect all arguments from
the command line:
/* Store up to 50 characters in our string buffer: */
#define BUFFER_SIZE 50
/* Store the argument (item) type value here: */
LONG item_type;
/* Temporary string buffer to store the argument name in: */
UBYTE item_name[ BUFFER_SIZE ];
- - -
/* Collect the first argument: */
item_type = ReadItem( item_name, BUFFER_SIZE, NULL );
/* As long as we find arguments we stay in the while loop: */
while( item_type )
{
/* Print the item type: */
switch( item_type )
{
case ITEM_EQUAL: printf( "Equal symbol " ); break;
case ITEM_ERROR: printf( "Item ERROR " ); break;
case ITEM_UNQUOTED: printf( "Unquoted item " ); break;
case ITEM_QUOTED: printf( "Quoted item " ); break;
default: printf( "Unknown item! " );
}
/* Print the item string: */
printf( "%s\n", item_name );
/* Collect next item: */
item_type = ReadItem( item_name, BUFFER_SIZE, NULL );
}
See also: AllocDosObject(), FindArg(), FreeArgs(),
FreeDosObject(), ReadArgs()
---------------------------------------------------------------